package com.hydl.power.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.github.pagehelper.PageInfo;
import com.hydl.power.aop.annotation.LogAnnotation;
import com.hydl.power.mapper.IncomingCabinetMapper;
import com.hydl.power.mapper.PowerRoomMapper;
import com.hydl.power.pojo.*;
import com.hydl.power.service.*;
import com.hydl.power.utils.EasyPoiUtil;
import com.hydl.power.utils.IdUtil;
import com.hydl.power.utils.Resp;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author wxb
 * @date 2020/4/23 16:27
 */
@Api(value = "PowerDataEnergyController",tags = "用电分析(用能报表/电力数据)")
@RequestMapping("/powerDataEnergy")
@RestController
@CrossOrigin
public class PowerDataEnergyController {
    @Autowired
    private PowerDataEnergyService energyService;
    @Autowired
    private PowerDataService powerDataService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private DemadReportService demadReportService;
    @Autowired
    private IncomingCabinetService incomingCabinetService;



    //@LogAnnotation(module = "用电分析(用能报表/电力数据)",description = "日报")
    @ApiOperation("用能报表/日报 energyForward(总计(kWh)),  deviceName(回路名称)")
    @PostMapping("/listByDay")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "2020-06-05(传这种格式)",defaultValue = "2020-11-16" ,dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "areaId", value = "区域id",defaultValue = "0371",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id",defaultValue = "40232328",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "powerRoomId", value = "配电房id",defaultValue = "56149022",dataType = "String"),
    })
    public Resp listByDay(String startTime,String areaId,String projectId,String powerRoomId) {
        List<List<PowerConsume>> lists = energyService.listByDay(startTime, areaId, projectId, powerRoomId);
        return Resp.success(lists);
    }


    // @LogAnnotation(module = "用电分析(用能报表/电力数据)",description = "周报")
    @ApiOperation("用能报表/周报 energyForward(总计(kWh)),  deviceName(回路名称)")
    @PostMapping("/listByWeek")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "2020-06-01(传这种格式)",defaultValue = "2020-06-01" ,dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "areaId", value = "区域id",defaultValue = "78371788",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id",defaultValue = "50642475",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "powerRoomId", value = "配电房id",defaultValue = "70980664",dataType = "String"),
    })
    public Resp listByWeek(String startTime,String areaId,String projectId,String powerRoomId) throws ParseException {

        List<List<PowerConsume>> lists = energyService.listByWeek(startTime, areaId, projectId, powerRoomId);
        return Resp.success(lists);
    }


    // @LogAnnotation(module = "用电分析(用能报表/电力数据)",description = "月报")
    @ApiOperation("用能报表/月报 energyForward(总计(kWh)),  deviceName(回路名称)")
    @PostMapping("/listByMonth")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "开始时间2021-06-01",defaultValue = "2020-03" ,dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "areaId", value = "区域id",defaultValue = "0371",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id",defaultValue = "40232328",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "powerRoomId", value = "配电房id",defaultValue = "56149022",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间",defaultValue = "2021-06-30",dataType = "String"),
    })
    public Resp listByMonth(String startTime,String areaId,String projectId,String powerRoomId,String endTime) throws ParseException {
        List<List<PowerConsume>> lists = energyService.listByMonths(startTime, areaId, projectId, powerRoomId, endTime);
        return Resp.success(lists);
    }


    // @LogAnnotation(module = "用电分析(用能报表/电力数据)",description = "年报")
    @ApiOperation("用能报表/年报 energyForward(总计(kWh)),  deviceName(回路名称)")
    @PostMapping("/listByYear")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "开始时间2020(传这种格式)",defaultValue = "2020-06-01" ,dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "areaId", value = "区域id",defaultValue = "78371788",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id",defaultValue = "50642475",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "powerRoomId", value = "配电房id",defaultValue = "70980664",dataType = "String"),
    })
    public Resp listByYear(String startTime,String areaId,String projectId,String powerRoomId) {
        List<List<PowerConsume>> lists = energyService.listByYear(startTime, areaId, projectId, powerRoomId);
        return Resp.success(lists);
    }







    //@LogAnnotation(module = "用电分析(用能报表/电力数据)",description = "电力数据/五个接口")
    @ApiOperation("电力数据/五个接口")
    @PostMapping("/listByPde")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "beginTime", value = "开始时间",defaultValue = "2020-4-26 10:10:41" ,dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间",defaultValue = "2020-4-26 11:01:41",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "areaId", value = "区域id",defaultValue = "78371788",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id",defaultValue = "50642475",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "powerRoomId", value = "配电房id",defaultValue = "70980664",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "deviceId", value = "设备id",defaultValue = "20021473",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "flag", value = "1有功总电能 2电流 3相电压 4线电压 5功率因数",dataType = "Integer"),
    })
    public Resp<List<PowerDataEnergy>> listByPde(String beginTime,String endTime,String areaId,String projectId,String powerRoomId,String deviceId,Integer flag){
        List<PowerDataEnergy> list = energyService.listByPde(IdUtil.str2Long(beginTime),IdUtil.str2Long(endTime),areaId,projectId,powerRoomId,deviceId,flag);
        return Resp.success(list);
    }


    //@LogAnnotation(module = "用电分析(用能报表/电力数据)",description = "电力数据/五个接口导出Excel")
    @ApiOperation("电力数据/五个接口导出Excel")
    @PostMapping("/listPdeByExcel")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "beginTime", value = "开始时间",defaultValue = "2020-4-26 10:10:41" ,dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间",defaultValue = "2020-4-26 15:01:41",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "areaId", value = "区域id",defaultValue = "78371788",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id",defaultValue = "50642475",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "powerRoomId", value = "配电房id",defaultValue = "70980664",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "deviceId", value = "设备id",defaultValue = "20021473",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "flag", value = "1有功总电能 2电流 3相电压 4线电压 5功率因数",dataType = "Integer"),
    })
    public void listPtByExcel(HttpServletResponse response, String beginTime, String endTime, String areaId, String projectId, String powerRoomId, String deviceId, Integer flag){
        Long begin = null;
        Long end = null;
        if (StringUtils.isNotEmpty(beginTime)) {
            begin = IdUtil.str2Long(beginTime);
        }
        if (StringUtils.isNotEmpty(endTime)) {
            end = IdUtil.str2Long(endTime);
        }
        Device device = deviceService.selectBydeviceId(deviceId);
        if (flag == 1){
            List<PowerDataEnergy> powerDataEnergyList = energyService.listPtByList(begin,end,areaId,projectId,powerRoomId,deviceId);
            String fileName = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + device.getAreaName()+"_"+device.getPowerRoomName()+"_"+device.getPowerRoomName()+"_"+device.getDeviceName()+"有功总电能数据工单";
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
            HSSFSheet sheet = hssfWorkbook.createSheet();
            HSSFRow title = sheet.createRow(0);
            title.createCell((short) 0).setCellValue(new HSSFRichTextString("配电房名称"));
            title.createCell((short) 1).setCellValue(new HSSFRichTextString("回路名称"));
            title.createCell((short) 2).setCellValue(new HSSFRichTextString("采集时间"));
            title.createCell((short) 3).setCellValue(new HSSFRichTextString("E(kWh)"));
            for (int i = 1; i <= powerDataEnergyList.size(); i++) {
                PowerDataEnergy powerDataEnergy = powerDataEnergyList.get(i - 1);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time1 = sdf.format(new Date(powerDataEnergy.getCreateTime()));
                HSSFRow title1 = sheet.createRow(i);
                title1.createCell((short) 0).setCellValue(new HSSFRichTextString(powerDataEnergy.getPowerRoomName()));
                title1.createCell((short) 1).setCellValue(new HSSFRichTextString(powerDataEnergy.getDeviceName()));
                title1.createCell((short) 2).setCellValue(new HSSFRichTextString(time1));
                title1.createCell((short) 3).setCellValue(new HSSFRichTextString(powerDataEnergy.getEnergyForward().toString()));
            }
            try {
                OutputStream outputStream = response.getOutputStream();
                response.reset();
                response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xls").getBytes("utf-8"), "iso-8859-1"));
                response.setContentType("application/msexcel");
                hssfWorkbook.write(outputStream);
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if (flag == 2){
            List<PowerDataEnergy> powerDataEnergyList = energyService.listDlByList(begin,end,areaId,projectId,powerRoomId,deviceId);
            String fileName = new SimpleDateFormat("yyyy-MM-dd ").format(new Date()) + device.getAreaName()+"_"+device.getPowerRoomName()+"_"+device.getPowerRoomName()+"_"+device.getDeviceName()+"电流数据工单";
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
            HSSFSheet sheet = hssfWorkbook.createSheet();
            HSSFRow title = sheet.createRow(0);
            title.createCell((short) 0).setCellValue(new HSSFRichTextString("配电房名称"));
            title.createCell((short) 1).setCellValue(new HSSFRichTextString("回路名称"));
            title.createCell((short) 2).setCellValue(new HSSFRichTextString("采集时间"));
            title.createCell((short) 3).setCellValue(new HSSFRichTextString("Ia(A)"));
            title.createCell((short) 4).setCellValue(new HSSFRichTextString("Ia(B)"));
            title.createCell((short) 5).setCellValue(new HSSFRichTextString("Ia(C)"));
            for (int i = 1; i <= powerDataEnergyList.size(); i++) {
                PowerDataEnergy powerDataEnergy = powerDataEnergyList.get(i - 1);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time1 = sdf.format(new Date(powerDataEnergy.getCreateTime()));
                HSSFRow title1 = sheet.createRow(i);
                title1.createCell((short) 0).setCellValue(new HSSFRichTextString(powerDataEnergy.getPowerRoomName()));
                title1.createCell((short) 1).setCellValue(new HSSFRichTextString(powerDataEnergy.getDeviceName()));
                title1.createCell((short) 2).setCellValue(new HSSFRichTextString(time1));
                title1.createCell((short) 3).setCellValue(new HSSFRichTextString(powerDataEnergy.getEnergyIa().toString()));
                title1.createCell((short) 4).setCellValue(new HSSFRichTextString(powerDataEnergy.getEnergyIb().toString()));
                title1.createCell((short) 5).setCellValue(new HSSFRichTextString(powerDataEnergy.getEnergyIc().toString()));
            }
            try {
                OutputStream outputStream = response.getOutputStream();
                response.reset();
                response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xls").getBytes("utf-8"), "iso-8859-1"));
                response.setContentType("application/msexcel");
                hssfWorkbook.write(outputStream);
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if (flag == 3){
            List<PowerDataEnergy> powerDataEnergyList = energyService.listXiangDyByList(begin,end,areaId,projectId,powerRoomId,deviceId);
            String fileName = new SimpleDateFormat("yyyy-MM-dd ").format(new Date()) +  device.getAreaName()+"_"+device.getPowerRoomName()+"_"+device.getPowerRoomName()+"_"+device.getDeviceName()+"相电压数据工单";
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
            HSSFSheet sheet = hssfWorkbook.createSheet();
            HSSFRow title = sheet.createRow(0);
            title.createCell((short) 0).setCellValue(new HSSFRichTextString("配电房名称"));
            title.createCell((short) 1).setCellValue(new HSSFRichTextString("回路名称"));
            title.createCell((short) 2).setCellValue(new HSSFRichTextString("采集时间"));
            title.createCell((short) 3).setCellValue(new HSSFRichTextString("Ua(V)"));
            title.createCell((short) 4).setCellValue(new HSSFRichTextString("Ub(V)"));
            title.createCell((short) 5).setCellValue(new HSSFRichTextString("Uc(V)"));
            for (int i = 1; i <= powerDataEnergyList.size(); i++) {
                PowerDataEnergy powerDataEnergy = powerDataEnergyList.get(i - 1);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time1 = sdf.format(new Date(powerDataEnergy.getCreateTime()));
                HSSFRow title1 = sheet.createRow(i);
                title1.createCell((short) 0).setCellValue(new HSSFRichTextString(powerDataEnergy.getPowerRoomName()));
                title1.createCell((short) 1).setCellValue(new HSSFRichTextString(powerDataEnergy.getDeviceName()));
                title1.createCell((short) 2).setCellValue(new HSSFRichTextString(time1));
                title1.createCell((short) 3).setCellValue(new HSSFRichTextString(powerDataEnergy.getEnergyUa().toString()));
                title1.createCell((short) 4).setCellValue(new HSSFRichTextString(powerDataEnergy.getEnergyUb().toString()));
                title1.createCell((short) 5).setCellValue(new HSSFRichTextString(powerDataEnergy.getEnergyUc().toString()));
            }
            try {
                OutputStream outputStream = response.getOutputStream();
                response.reset();
                response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xls").getBytes("utf-8"), "iso-8859-1"));
                response.setContentType("application/msexcel");
                hssfWorkbook.write(outputStream);
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if (flag == 4){
            List<PowerDataEnergy> powerDataEnergyList = energyService.listXianDyByList(begin,end,areaId,projectId,powerRoomId,deviceId);
            String fileName = new SimpleDateFormat("yyyy-MM-dd ").format(new Date()) + device.getAreaName()+"_"+device.getPowerRoomName()+"_"+device.getPowerRoomName()+"_"+device.getDeviceName()+"线电压数据工单";
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
            HSSFSheet sheet = hssfWorkbook.createSheet();
            HSSFRow title = sheet.createRow(0);
            title.createCell((short) 0).setCellValue(new HSSFRichTextString("配电房名称"));
            title.createCell((short) 1).setCellValue(new HSSFRichTextString("回路名称"));
            title.createCell((short) 2).setCellValue(new HSSFRichTextString("采集时间"));
            title.createCell((short) 3).setCellValue(new HSSFRichTextString("Uab(V)"));
            title.createCell((short) 4).setCellValue(new HSSFRichTextString("Ubc(V)"));
            title.createCell((short) 5).setCellValue(new HSSFRichTextString("Uca(V)"));
            for (int i = 1; i <= powerDataEnergyList.size(); i++) {
                PowerDataEnergy powerDataEnergy = powerDataEnergyList.get(i - 1);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time1 = sdf.format(new Date(powerDataEnergy.getCreateTime()));
                HSSFRow title1 = sheet.createRow(i);
                title1.createCell((short) 0).setCellValue(new HSSFRichTextString(powerDataEnergy.getPowerRoomName()));
                title1.createCell((short) 1).setCellValue(new HSSFRichTextString(powerDataEnergy.getDeviceName()));
                title1.createCell((short) 2).setCellValue(new HSSFRichTextString(time1));
                title1.createCell((short) 3).setCellValue(new HSSFRichTextString(powerDataEnergy.getEnergyUab().toString()));
                title1.createCell((short) 4).setCellValue(new HSSFRichTextString(powerDataEnergy.getEnergyUbc().toString()));
                title1.createCell((short) 5).setCellValue(new HSSFRichTextString(powerDataEnergy.getEnergyUca().toString()));
            }
            try {
                OutputStream outputStream = response.getOutputStream();
                response.reset();
                response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xls").getBytes("utf-8"), "iso-8859-1"));
                response.setContentType("application/msexcel");
                hssfWorkbook.write(outputStream);
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if (flag == 5){
            List<PowerDataEnergy> powerDataEnergyList = energyService.listPfByList(begin,end,areaId,projectId,powerRoomId,deviceId);
            String fileName = new SimpleDateFormat("yyyy-MM-dd ").format(new Date()) + device.getAreaName()+"_"+device.getPowerRoomName()+"_"+device.getPowerRoomName()+"_"+device.getDeviceName()+"功率因数数据工单";
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
            HSSFSheet sheet = hssfWorkbook.createSheet();
            HSSFRow title = sheet.createRow(0);
            title.createCell((short) 0).setCellValue(new HSSFRichTextString("配电房名称"));
            title.createCell((short) 1).setCellValue(new HSSFRichTextString("回路名称"));
            title.createCell((short) 2).setCellValue(new HSSFRichTextString("采集时间"));
            title.createCell((short) 3).setCellValue(new HSSFRichTextString("PFa"));
            title.createCell((short) 4).setCellValue(new HSSFRichTextString("PFb"));
            title.createCell((short) 5).setCellValue(new HSSFRichTextString("PFc"));
            title.createCell((short) 6).setCellValue(new HSSFRichTextString("PFav"));
            for (int i = 1; i <= powerDataEnergyList.size(); i++) {
                PowerDataEnergy powerDataEnergy = powerDataEnergyList.get(i - 1);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time1 = sdf.format(new Date(powerDataEnergy.getCreateTime()));
                HSSFRow title1 = sheet.createRow(i);
                title1.createCell((short) 0).setCellValue(new HSSFRichTextString(powerDataEnergy.getPowerRoomName()));
                title1.createCell((short) 1).setCellValue(new HSSFRichTextString(powerDataEnergy.getDeviceName()));
                title1.createCell((short) 2).setCellValue(new HSSFRichTextString(time1));
                title1.createCell((short) 3).setCellValue(new HSSFRichTextString(powerDataEnergy.getEnergyPaf().toString()));
                title1.createCell((short) 4).setCellValue(new HSSFRichTextString(powerDataEnergy.getEnergyPbf().toString()));
                title1.createCell((short) 5).setCellValue(new HSSFRichTextString(powerDataEnergy.getEnergyPcf().toString()));
                title1.createCell((short) 6).setCellValue(new HSSFRichTextString(powerDataEnergy.getEnergyPtf().toString()));
            }
            try {
                OutputStream outputStream = response.getOutputStream();
                response.reset();
                response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xls").getBytes("utf-8"), "iso-8859-1"));
                response.setContentType("application/msexcel");
                hssfWorkbook.write(outputStream);
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    // @LogAnnotation(module = "设备电力数据",description = "电力数据")
    @ApiOperation("设备电力数据")
    @PostMapping("/queryPowerData")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", value = "设备编码",defaultValue = "" ,dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "time", value = "时间",defaultValue = "2020--26 11:01:41",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "dataType", value = "类型",defaultValue = "",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "minute", value = "分",defaultValue = "",dataType = "String"),
    })
    public Resp queryPowerData(String deviceId, String time, String dataType,String minute){
        return Resp.success(powerDataService.queryPowerData(deviceId,time,dataType,minute));
    }

    /*<!--查詢所有设备-->*/
    @ApiOperation("查询所有设备数据")
    @PostMapping("/queryProwerAllDevices")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数",defaultValue = "" ,dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "页大小",defaultValue = "",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "单个项目Id",defaultValue = "" ,dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "time", value = "时间",defaultValue = "2020--26 11:01:41",dataType = "String"),
    })
    public Resp<PageInfo<Powerdata>> queryProwerAllDevices(Integer pageNum,Integer pageSize,String projectId, String time){
        PageInfo<Powerdata> list = powerDataService.queryProwerAllDevices( pageNum, pageSize,projectId, time);
        return Resp.success(list);
    }

    /*<!--所有设备数据导出-->*/
    @ApiOperation("所有设备数据导出")
    @GetMapping("/queryPowerAllExport")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "单个项目Id",defaultValue = "" ,dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "time", value = "时间",defaultValue = "2020--26 11:01:41",dataType = "String"),
    })
    public void queryPowerAllExport(String projectId, String time,HttpServletResponse response) throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        List<Powerdata> list = powerDataService.queryPowerAllExport(projectId, time);
        //创建全局变量 colList为列集合，list1为值得集合
        List<ExcelExportEntity> colList = new ArrayList<>();
        Map<String, Object> valMap = new HashMap<String, Object>();
        List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
        //循环list集合
        for (Powerdata powerdata : list) {
            //清空map
            valMap = new HashMap<String, Object>();
            //清空列得值
            colList = new ArrayList<>();
            //添加设备名称列 保证下方循环时不为空
            ExcelExportEntity colEntity = new ExcelExportEntity("设备名称","deviceName");
            colEntity.setNeedMerge(true);
            colEntity.setWidth(40);
            colList.add(colEntity);
            valMap.put("deviceName", powerdata.getDeviceName());
            //添加采集时间列
            colEntity = new ExcelExportEntity("采集时间","time");
            colEntity.setNeedMerge(true);
            colEntity.setWidth(30);
            colList.add(colEntity);
            valMap.put("time",powerdata.getTime());

            //将对象属性转化为数组
            Field[] field = powerdata.getClass().getDeclaredFields();
            for (int j=0 ; j<field.length ; j++) {
                String name = field[j].getName();    //获取属性的名字
                name = name.substring(0,1).toUpperCase()+name.substring(1); //将属性的首字符大写，方便构造get，set方法
                String type = field[j].getGenericType().toString();    //获取属性的类型
                //判断属性得类型，如果符合，get出值
                if(type.equals("class java.lang.Float")){
                    Method ms = powerdata.getClass().getMethod("get"+name);
                    Float values = (Float) ms.invoke(powerdata);
                    //判断值是否为空
                    if(values != null){
                        //上方已添加数据，不存在为空报错得现象，如果列与值相同，则不添加列，不存在则添加
                        for (ExcelExportEntity excelExportEntity : colList) {
                            if (excelExportEntity.getName().equals(name)) {
                                System.out.println("已存在列");
                            }
                        }
                        colEntity = new ExcelExportEntity(name,name);
                        System.out.println();
                        colEntity.setNeedMerge(true);
                        colList.add(colEntity);
                        valMap.put(name,values);
                    }
                }
            }
            list1.add(valMap);
        }
        System.out.println(colList);
        System.out.println(list1);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("电力数据", "电力数据"), colList,list1);
        OutputStream outstream = response.getOutputStream();
        response.reset();
        response.setHeader("Content-disposition", "attachment; filename=" + new String("电力数据".getBytes(),"iso-8859-1") + ".xls");
        response.setContentType("application/x-download");
        workbook.write(outstream);
        outstream.close();
    }


    //   @LogAnnotation(module = "设备电力数据导出",description = "电力数据导出")
    @ApiOperation("设备电力数据导出")
    @GetMapping("/queryPowerDataExport")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", value = "设备编码",defaultValue = "" ,dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "time", value = "时间",defaultValue = "2020-02-06 11:01:41",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "dataType", value = "类型",defaultValue = "",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "minute", value = "分",defaultValue = "",dataType = "String"),
    })
    public void queryPowerDataExport(String deviceId, String time, String dataType,String minute,HttpServletResponse response)throws Exception{
        //获取list集合
         List<Powerdata> list = powerDataService.queryPowerData(deviceId,time,dataType,minute);
         //创建全局变量 colList为列集合，list1为值得集合
        List<ExcelExportEntity> colList = new ArrayList<>();
        Map<String, Object> valMap = new HashMap<String, Object>();
        List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
        //循环list集合
        for (Powerdata powerdata : list) {
            System.out.println(powerdata.getTime());
            //清空map
            valMap = new HashMap<String, Object>();
            //清空列得值
            colList = new ArrayList<>();
            //添加设备名称列 保证下方循环时不为空
            ExcelExportEntity colEntity = new ExcelExportEntity("设备名称",powerdata.getDeviceName());
            colEntity.setNeedMerge(true);
            colEntity.setWidth(40);
            colList.add(colEntity);
            valMap.put(powerdata.getDeviceName(), powerdata.getDeviceName());
            //添加采集时间列
            colEntity = new ExcelExportEntity("采集时间","time");
            colEntity.setNeedMerge(true);
            colEntity.setWidth(30);
            colList.add(colEntity);
            valMap.put("time",powerdata.getTime());

            //将对象属性转化为数组
            Field[] field = powerdata.getClass().getDeclaredFields();
            for (int j=0 ; j<field.length ; j++) {
                String name = field[j].getName();    //获取属性的名字
                name = name.substring(0,1).toUpperCase()+name.substring(1); //将属性的首字符大写，方便构造get，set方法
                String type = field[j].getGenericType().toString();    //获取属性的类型
                //判断属性得类型，如果符合，get出值
                if(type.equals("class java.lang.Float")){
                    Method ms = powerdata.getClass().getMethod("get"+name);
                    Float values = (Float) ms.invoke(powerdata);
                    //判断值是否为空
                    if(values != null){
                        //上方已添加数据，不存在为空报错得现象，如果列与值相同，则不添加列，不存在则添加
                            for (ExcelExportEntity excelExportEntity : colList) {
                                if (excelExportEntity.getName().equals(name)) {
                                    System.out.println("已存在列");
                                }
                            }
                            colEntity = new ExcelExportEntity(name,name);
                            colEntity.setNeedMerge(true);
                            colList.add(colEntity);
                            valMap.put(name, values );
                    }
                    }
            }
            list1.add(valMap);
        }
        System.out.println(colList);
        System.out.println(list1);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("电力数据", "电力数据"), colList,list1);
        OutputStream outstream = response.getOutputStream();
        response.reset();
        response.setHeader("Content-disposition", "attachment; filename=" + new String("电力数据".getBytes(),"iso-8859-1") + ".xls");
        response.setContentType("application/x-download");
        workbook.write(outstream);
        outstream.close();

        //EasyPoiUtil.exportExcel(EasyPoiUtil.getWorkbook("电力数据", "电力数据", Powerdata.class, list), "电力数据", response);
    }

    //  @LogAnnotation(module = "需求报告",description = "需求报告")
    @ApiOperation("需求报告")
    @PostMapping("/queryDemadReport")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "开始时间",defaultValue = "" ,dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间",defaultValue = "2020-02-06 11:01:41",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "token", value = "token",defaultValue = "",dataType = "String"),
    })
    public Resp queryDemadReport( String startTime, String endTime,String token){
        return Resp.success(demadReportService.queryDemadReport(startTime,endTime,token));
    }

    //@LogAnnotation(module = "变压器监测",description = "变压器监测")
    @ApiOperation("变压器监测")
    @PostMapping("/queryTransformerMonitor")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", value = "设备编码",defaultValue = "56149022" ,dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "time", value = "时间",defaultValue = "2020-12-10",dataType = "String"),
        })
    public Resp queryTransformerMonitor(String deviceId, String time) throws ParseException{
      List list=incomingCabinetService.queryIncoming(time,deviceId);
      if(list.size()==0){
        return Resp.success("没有查询到数据",list,null);
      }
      return Resp.success(list);
    }

    //  @LogAnnotation(module = "变压器监测上一日数据",description = "变压器监测上一日数据")
    @ApiOperation("变压器监测上一日数据")
    @PostMapping("/queryYesterday")
    @ApiImplicitParams({
      @ApiImplicitParam(paramType = "query", name = "deviceId", value = "设备编码",defaultValue = "56149022" ,dataType = "String"),
      @ApiImplicitParam(paramType = "query", name = "time", value = "时间",defaultValue = "2020-12-10",dataType = "String"),
    })
    public Resp queryYesterday(String deviceId, String time) throws ParseException{
      List list=incomingCabinetService.queryYesterdayIncoming(time,deviceId);
      System.out.println(list.size());
      if(list.size()==0){
        return Resp.success("没有查询到数据",list,null);
      }
      return Resp.success(list);
    }

    /*
    *动态导出列模板
    * */
/*    @ApiOperation("11111")
    @GetMapping("/qwe")
    public void qwe(HttpServletResponse response) throws Exception{
        List<ExcelExportEntity> colList = new ArrayList<>();
        ExcelExportEntity colEntity = new ExcelExportEntity("航司", "airlinecode");
        colEntity.setNeedMerge(true);
        //设置列得宽
        //colEntity.setWidth(50);
        colList.add(colEntity);
        //添加列
        colEntity = new ExcelExportEntity("PNR", "pnr");
        colEntity.setNeedMerge(true);
        colList.add(colEntity);

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < 10; i++) {
            Map<String, Object> valMap = new HashMap<String, Object>();
            valMap.put("airlinecode", "111." );
            valMap.put("pnr", "222." );
            list.add(valMap);
        }
        System.out.println(list);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("价格分析表1", "数据"), colList,list);
        OutputStream outstream = response.getOutputStream();
        response.reset();
        response.setHeader("Content-disposition", "attachment; filename=" + new String("价格分析表1".getBytes(),"iso-8859-1") + ".xls");
        response.setContentType("application/x-download");
        workbook.write(outstream);
        outstream.close();
    }*/

    @ApiOperation("用能分类")
    @PostMapping("/queryLoopType")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目Id",defaultValue = "40232328" ,dataType = "String"),
    })
    public Resp queryLoopType(String projectId){
        List<LoopType> list = deviceService.queryLoopType(projectId);
        if(list.size() == 0){
            return Resp.success("未查询到数据",null,null);
        }
        return Resp.success(list);
    }

    @ApiOperation("年用能分类")
    @PostMapping("/queryYearLoopType")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目Id",defaultValue = "40232328" ,dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "createTime", value = "开始时间",defaultValue = "2021" ,dataType = "String")
    })
    public Resp queryYearLoopType(String projectId ,String createTime){
        List<LoopType> list = deviceService.queryYearLoopType(projectId,createTime);
        if(list.size() == 0){
            return Resp.success("未查询到数据",null,null);
        }
        return Resp.success(list);
    }


    @ApiOperation("月用能分类")
    @PostMapping("/queryMonthLoopType")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目Id",defaultValue = "40232328" ,dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "createTime", value = "开始时间",defaultValue = "2021" ,dataType = "String")
    })
    public Resp queryMonthLoopType(String projectId ,String createTime){
        List<LoopType> list = deviceService.queryMonthLoopType(projectId,createTime);
        if(list.size() == 0){
            return Resp.success("未查询到数据",null,null);
        }
        return Resp.success(list);
    }
}
